package io.github.wjehovah.yyn.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.activity.LoginRegistActivity;
import io.github.wjehovah.yyn.bean.MineBean;
import io.github.wjehovah.yyn.data.Budejie;
import io.github.wjehovah.yyn.utils.DimensionUtil;
import io.github.wjehovah.yyn.utils.SharedPrefUtils;

/**
 * 点击底部 tab 对应的页面
 */
public class BaseFragment extends Fragment implements View.OnClickListener {
/*    @ViewInject(R.id.login_regist)
    private LinearLayout ll_mine_regist;//注册点击；
    @ViewInject(R.id.mine_download)
    private LinearLayout ll_mine_offline_down;//离线下载；
    @ViewInject(R.id.gv_mine)
    private GridView mGv_mine;*/

    @ViewInject(R.id.indicator_essence)
    private TabPageIndicator mEssenceIndicator;

    @ViewInject(R.id.viewpager_essence)
    private ViewPager mEssenceViewPager;


    @ViewInject(R.id.viewpager_last)
    private ViewPager mLastViewPager;

    @ViewInject(R.id.indicator_last)
    private TabPageIndicator mLastIndicator;


    @ViewInject(R.id.btn_focus_login)
    private Button mButtonLogin;//关注页面的登陆按钮

    @ViewInject(R.id.btn_focus_regist)
    private Button mButtonRegist;//关注页面的注册按钮

    private int index;//从 HomeGroup 传进来的底部 tab 位置
    private MineBean mineBean = new MineBean();

    private MyAdapter baseFragmentAdapter;

    private int historyPagePosition;


    public BaseFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //得到从 HomeGroup 传进来的底部 tab 位置
        index = getArguments().getInt(Budejie.POSITION);
        View view;
        String positionInfo = SharedPrefUtils.getString(Budejie.POSITION, null);
        if (positionInfo != null) {
            index = Integer.valueOf(positionInfo.split("_")[0]);
        }

        switch (index) {
            case 0:
                view = inflater.inflate(R.layout.fragment_bottom_essence, null, false);
                ViewUtils.inject(this, view);
                baseFragmentAdapter = new MyAdapter(getFragmentManager(), index);

                return view;
            case 1:
                view = inflater.inflate(R.layout.fragment_bottom_last, null, false);
                ViewUtils.inject(this, view);
                baseFragmentAdapter = new MyAdapter(getFragmentManager(), index);

                return view;

            case 4://我的页面
                view = inflater.inflate(R.layout.mine, null, false);
                ViewUtils.inject(this, view);
                return view;

            case 3: //关注
                view = inflater.inflate(R.layout.focus_view, null, false);
                ViewUtils.inject(this, view);
                //给关注页面的登陆 及 注册按钮添加点击监听
                mButtonLogin.setOnClickListener(this);
                mButtonRegist.setOnClickListener(this);

                //设置动画
                return view;

            default:
                view = new TextView(getActivity());
                TextView textView = (TextView) view;
                textView.setGravity(Gravity.CENTER);
                textView.setTextSize(DimensionUtil.dp2px(20));
                textView.setText("这是底部第" + index + "个tab 对应的 page");
                return view;
        }
    }

    private ViewPager.SimpleOnPageChangeListener mEssenceListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            BaseBudejieFragment budejieFragment = (BaseBudejieFragment) baseFragmentAdapter.instantiateItem(mEssenceViewPager, mEssenceViewPager.getCurrentItem());
            budejieFragment.buttonPressed();
        }
    };
    private ViewPager.SimpleOnPageChangeListener mLastListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            BaseBudejieFragment budejieFragment = (BaseBudejieFragment) baseFragmentAdapter.instantiateItem(mLastViewPager, mLastViewPager.getCurrentItem());
            budejieFragment.buttonPressed();
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        int currentPosition = 0;
        String positionInfo = SharedPrefUtils.getString(Budejie.POSITION, null);
        if (positionInfo != null) {
            currentPosition = Integer.valueOf(positionInfo.split("_")[1]);
        }

        //精华和最新
        switch (index) {
            case 0:
                mEssenceViewPager.setAdapter(baseFragmentAdapter);
                mEssenceIndicator.setViewPager(mEssenceViewPager);
                //mEssenceViewPager.setOffscreenPageLimit(4);
                mEssenceIndicator.setOnPageChangeListener(mEssenceListener);
                if (positionInfo != null) {
                    //    baseFragmentAdapter.instantiateItem(mEssenceViewPager, currentPosition);
                    //  mEssenceIndicator.setCurrentItem(currentPosition);
                }
                break;
            case 1:
                mLastViewPager.setAdapter(baseFragmentAdapter);
                mLastIndicator.setViewPager(mLastViewPager);
                // mLastViewPager.setOffscreenPageLimit(4);
                mLastIndicator.setOnPageChangeListener(mLastListener);
                if (positionInfo != null) {
                    // baseFragmentAdapter.instantiateItem(mLastViewPager, currentPosition);
                    // mLastIndicator.setCurrentItem(currentPosition);
                }
                break;
            case 4:
             /*   ll_mine_offline_down.setOnClickListener(this);
                ll_mine_regist.setOnClickListener(this);
                HttpUtils httpUtils = new HttpUtils();
                httpUtils.send(HttpRequest.HttpMethod.GET, Budejie.REQUEST_GRIDEVIEW, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String json_grideview = responseInfo.result;
                        Log.e("TAG", "请求grideview成功。。。。" + json_grideview);
                        Gson gson = new Gson();
                        mineBean = gson.fromJson(json_grideview, MineBean.class);
                        Log.e("TAG", "请求grideview成功。。。。" + mineBean.toString());


                        List<MineBean.Mine> data = mineBean.square_list;//请求网络获取grideview内容；
                        mGv_mine.setAdapter(new MineGrideViewAdaper(data, getActivity()));
                        mGv_mine.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                //
                                MineBean.Mine mine = mineBean.square_list.get(position);
                                Toast.makeText(getActivity(), "请求URL==" + mine.url, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        Log.e("TAG", "请求grideview失败。。。。");
                    }
                });*/

        }
    }


    /**
     * 关注页面的登陆 注册的点击回调事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        Intent mIntent = new Intent(getActivity(), LoginRegistActivity.class);
        switch (v.getId()) {
            case R.id.btn_focus_login:
                //Toast.makeText(getActivity(),"登陆",Toast.LENGTH_SHORT).show();
                //携带数据 标识登录
                mIntent.putExtra("ACTION", "login");
                startActivity(mIntent);
                break;

            case R.id.btn_focus_regist:
                //Toast.makeText(getActivity(),"注册",Toast.LENGTH_SHORT).show();
                mIntent.putExtra("ACTION", "regist");
                startActivity(mIntent);
                break;
            case R.id.login_regist:
                Toast.makeText(getActivity(), "启动注册或者登录的activity", Toast.LENGTH_SHORT).show();
                break;
            case R.id.mine_download:
                Toast.makeText(getActivity(), "启动离线下载的activity", Toast.LENGTH_SHORT).show();
                break;

            default:

                break;
        }

    }


    public static class MyAdapter extends FragmentStatePagerAdapter {
        public static int adapterId;

        public MyAdapter(FragmentManager fragmentManager, int id) {
            super(fragmentManager);
            adapterId = id;
            Log.e(Budejie.TAG + "adapter", adapterId + "");
        }

        //添加标题
        @Override
        public CharSequence getPageTitle(int position) {
            return Budejie.data[adapterId][position];
        }

        @Override
        public int getCount() {
            return Budejie.homeEssence.length;//根据具体数据显示
        }


        @Override
        public BaseBudejieFragment getItem(int position) {//position 为 viewPager  某个页面 id
            BaseBudejieFragment fragment;
            Log.e(Budejie.TAG + "---", position + "");
            switch (adapterId) {//adapterId对应底部 tabPosition
                case 0://精华
                    fragment = BaseBudejieFragment.newInstance("essence", String.valueOf(position));

                    return fragment;
                case 1://最新
                    fragment = BaseBudejieFragment.newInstance("last", String.valueOf(position));

                    return fragment;
                default://其他的三个
                    //Todo
                    fragment = BaseBudejieFragment.newInstance("other", String.valueOf(position));
                    return fragment;
            }

        }

    }
}
